package bio.four;

import java.io.*;
import java.net.Socket;

/*
    发送文件的客户端
 */
public class Client {
    public static void main(String[] args) {
        try (
                FileInputStream fis = new FileInputStream("C:\\Users\\yfq\\Desktop\\1.jpg");
        ) {
            Socket socket = new Socket("localhost", 9999);
            //封装数据输出流
            DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
            //发送文件的后缀
            dos.writeUTF(".jpg");
            //发送文件
            byte[] buf = new byte[1024];
            int len = 0;
            while ((len = fis.read(buf)) != -1) {
                //写入数据
                dos.write(buf, 0, len);
            }
            dos.flush();
            //结束输出流
            socket.shutdownOutput();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
